<template>
  <el-dialog v-el-drag-dialog :title="!form.clientId?'新建授权信息':'编辑授权信息'" :visible.sync="visible" :append-to-body="appendToBody" :before-close="onClose">
    <el-form ref="oauthForm" :model="form" label-width="120px" :disabled="!hasLoaded">
      <el-card>
        <el-form-item hidden>
          <el-input v-model="form.autoapprove" hidden />
          <el-input v-model="form.accessTokenValidity" hidden />
        </el-form-item>
        <el-form-item label="Client ID" prop="clientId">
          <el-input v-model="form.clientId" :readonly="readonly" />
        </el-form-item>
        <el-form-item label="租户名称" prop="clientName">
          <el-input v-model="form.clientName" autocomplete="off" />
        </el-form-item>
        <el-form-item label="资源限定串" prop="resourceIds">
          <el-input v-model="form.resourceIds" autocomplete="off" />
        </el-form-item>
        <el-form-item label="密钥" prop="clientSecretStr">
          <el-input v-model="form.clientSecretStr" autocomplete="off" type="password" show-password />
        </el-form-item>
        <el-form-item label="范围" prop="scope">
          <el-input v-model="form.scope" autocomplete="off" />
        </el-form-item>
        <el-form-item label="token有效期" prop="accessTokenValidity">
          <el-input-number v-model="form.accessTokenValidity" :min="0" controls-position="right" />
        </el-form-item>
        <el-form-item label="refresh有效期" prop="refreshTokenValidity">
          <el-input-number v-model="form.refreshTokenValidity" :min="0" controls-position="right" />
        </el-form-item>
        <el-form-item label="授权方式" prop="authorizedGrantTypesSelects">
          <el-checkbox-group v-model="form.authorizedGrantTypesSelects">
            <el-checkbox
              v-for="type in optionOfTypes"
              :key="type.dictLabel + '_' + type.dictValue"
              :label="type.dictValue"
            >
              {{ type.dictLabel }}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="回调地址" prop="webServerRedirectUri">
          <el-input v-model="form.webServerRedirectUri" />
        </el-form-item>
      </el-card>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button v-if="hasLoaded" type="primary" icon="el-icon-check" @click="onSubmit">确 定</el-button>
      <el-button icon="el-icon-close" @click="onClose">关 闭</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { oauthLoad, oauthSave } from '@/api/system/oauth'
import { dictGroup } from '@/api/system/dict'

export default {
  name: 'ClientFormDialog',
  data() {
    return {
      visible: false,
      readonly: false,
      appendToBody: false,
      hasLoaded: false,
      form: {
        clientId: undefined,
        autoapprove: 'true',
        accessTokenValidity: 7200,
        refreshTokenValidity: 0,
        clientName: undefined,
        resourceIds: undefined,
        clientSecretStr: undefined,
        scope: undefined,
        authorizedGrantTypes: undefined,
        authorizedGrantTypesSelects: [],
        webServerRedirectUri: undefined
      },
      optionOfTypes: []
    }
  },
  methods: {
    init(clientId) {
      this.hasLoaded = false
      this.form.clientId = clientId || null
      this.visible = true
      this.$nextTick(() => {
        this.loadForm(clientId)
        this.getDictData()
      })
    },
    loadForm(clientId) {
      if (clientId) {
        this.readonly = true
        oauthLoad({ 'id': clientId }).then(response => {
          this.form = this.$api.objectUpdate(this.form, response.param)
          if (this.form.authorizedGrantTypes) {
            this.form.authorizedGrantTypesSelects = this.form.authorizedGrantTypes.split(',')
          }
        }).catch(() => {})
      }

      // 判断当前用户权限
      this.hasLoaded = this.$perm.hasPermissionOr(['system:oauth:edit'])
    },
    getDictData() {
      dictGroup({ client: 'AuthorizedGrantType' }).then(response => {
        this.optionOfTypes = response
      }).catch(e => {})
    },
    onSubmit() {
      this.$refs.oauthForm.validate(valid => {
        if (valid) {
          if (this.form.authorizedGrantTypesSelects.length > 0) {
            this.form.authorizedGrantTypes = this.form.authorizedGrantTypesSelects.join(',')
          }
          oauthSave(this.form).then(response => {
            this.$modal.msgSuccess(response.msg)
            this.$emit('confirmForm')
            this.onClose()
          }).catch(() => {})
        }
      })
    },
    onClose() {
      this.$refs.oauthForm.resetFields()
      this.visible = false
      this.readonly = false
    }
  }
}
</script>
